/* 
   Copyright 2012 Ryne Okimoto

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package delegate;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import model.DataManagerModel;
import manager.FileManager;

/**
 * Class - SimpleMainViewController
 * Description - Main viewcontroller for the first iteration
 *               Contains two buttons to create and view.
 * Notes - Extend to have a filebrowser and recipe display
 *         in one view.
 * 
 * @author Ryne Okimoto
 * 
 * Inspired by Fred Swartz December 2004
 */

@SuppressWarnings("serial")
public class SimpleMainViewController extends JFrame 
{
	// Class data
	private DataManagerModel dataModel;
	private JButton btnCreate = new JButton("Create Recipe");
	private JButton btnOpen = new JButton("View Recipe");
	
	/**
	 * Constructor
	 * Description - Initializes and adds componenets to view
	 */
	public SimpleMainViewController(DataManagerModel model)
	{
		// Set model for the viewcontroller
		dataModel = model;
		
		// Set layout
		JPanel view = new JPanel();
		GridBagConstraints gbcBtnCreate = new GridBagConstraints();
		GridBagConstraints gbcBtnOpen = new GridBagConstraints();
		
		gbcBtnCreate.gridx = 0;
		gbcBtnCreate.gridy = 0;
		gbcBtnCreate.insets = new Insets(50, 50, 50, 25);
		
		gbcBtnOpen.gridx = 1;
		gbcBtnOpen.gridy = 0;
		gbcBtnOpen.insets = new Insets(50, 25, 50, 50);
		
		view.setLayout(new GridBagLayout());
		
		// Add components
		view.add(btnCreate, gbcBtnCreate);
		view.add(btnOpen, gbcBtnOpen);
		
		// Add listeners
		btnCreate.addActionListener(new CreateClickedListener());
		btnOpen.addActionListener(new OpenClickedListener());
		
		this.setContentPane(view);
		this.setTitle("LunchBox Main Menu");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		this.pack();
		
	} // Constructor
	
	/**
	 * Class - CreateClickedListener
	 * Description - Opens the create recipe view when the create
	 *               button is clicked
	 */
	private class CreateClickedListener implements ActionListener
	{
		/**
		 * Method - actionPerformed
		 * Description - opens the create recipe view
		 */
		public void actionPerformed(ActionEvent e)
		{
			// JOptionPane.showMessageDialog(SimpleMainViewController.this, "Clicked 'Create Recipe'");
			CreateViewController create = new CreateViewController(dataModel);
			create.setVisible(true);
		}
	} // CreateClickedListener
	
	/**
	 * Class - OpenClickedListener
	 * Description - Performs action when the open button is clicked
	 */
	private class OpenClickedListener implements ActionListener
	{
		/**
		 * Method - actionPerformed
		 * Description - opens the filechooser to select a recipe
		 */
		public void actionPerformed(ActionEvent e)
		{
			// JOptionPane.showMessageDialog(SimpleMainViewController.this, "Clicked 'View Recipe'");
			
			// For initial implementation use a JFileChooser
			// Later implementation will use another view and model
			
			JFileChooser fileBrowser = new JFileChooser("./rcp_db/Rcp_Files");
			FileNameExtensionFilter recipeFilter = new FileNameExtensionFilter("Recipe files (.rcp)", "rcp");
			fileBrowser.setFileFilter(recipeFilter);
			
			int option = fileBrowser.showOpenDialog(SimpleMainViewController.this);
			
			if(option == JFileChooser.APPROVE_OPTION){
				// Console output for debugging only
				//System.out.println(fileBrowser.getSelectedFile().getName() + " was chosen.");
				FileManager recipeFM = new FileManager(fileBrowser.getSelectedFile().getName(), 
						fileBrowser.getSelectedFile());
				
				OpenRecipeViewController recipeViewer = new OpenRecipeViewController(dataModel, recipeFM);
				recipeViewer.setVisible(true);
			}
		}
	} // OpenClickedListener
	
	/**
	 * Unit tester
	 * @param args
	 */
	public static void main(String[] args)
	{
		DataManagerModel model = new DataManagerModel();
		SimpleMainViewController viewControl = new SimpleMainViewController(model);
		viewControl.setVisible(true);
	} // main

} // SimpleMainViewController
